package config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;


@Configuration
@SuppressWarnings("all")
public class DirectRabbitConfig implements InitializingBean {
    @Resource
    private RabbitTemplate rabbitTemplate;
    //    @Resource
//    private RabbitAdmin rabbitAdmin;
    @Override
    public void afterPropertiesSet() {
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    }
    @Bean
    public MessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public MessageConverter jsonMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }
    @Bean
    public MessageConverter jsonToMapMessageConverter() {
        DefaultClassMapper defaultClassMapper = new DefaultClassMapper();
        defaultClassMapper.setTrustedPackages("*");
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
        jackson2JsonMessageConverter.setClassMapper(defaultClassMapper);
        return jackson2JsonMessageConverter;
    }
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory, MessageConverter jackson2JsonMessageConverter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jackson2JsonMessageConverter);
        return factory;
    }
//    @Bean
//    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
//        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
//        // 只有设置为 true，spring 才会加载 RabbitAdmin 这个类
//        rabbitAdmin.setAutoStartup(true);
//        return rabbitAdmin;
//    }

//    @Override
//    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        //创建交换机
//        rabbitAdmin.declareExchange(rabbitmqDirectExchange());
//        //创建队列
//        rabbitAdmin.declareQueue(rabbitmqDirectQueue());
//        return null;
//    }

    @Bean
    public Queue rabbitmqDirectQueue() {
        /**
         * 1、name:    队列名称
         * 2、durable: 是否持久化
         * 3、exclusive: 是否独享、排外的。如果设置为true，定义为排他队列。则只有创建者可以使用此队列。也就是private私有的。
         * 4、autoDelete: 是否自动删除。也就是临时队列。当最后一个消费者断开连接后，会自动删除。
         * */
        return new Queue(constant.CommonConstant.QUEUE_NAME, true, false, false);
    }

//    @Bean
//    public DirectExchange rabbitmqDirectExchange() {
//        //Direct交换机
//        return new DirectExchange(constant.CommonConstant.EXCHANGE_NAME, true, false);
//    }
//
//    @Bean
//    public Binding bindDirect() {
//        //链式写法，绑定交换机和队列，并设置匹配键
//        return BindingBuilder
//                //绑定队列
//                .bind(rabbitmqDirectQueue())
//                //到交换机
//                .to(rabbitmqDirectExchange())
//                //并设置匹配键
//                .with(constant.CommonConstant.ROUTING_KEY);
//    }
}
